﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;


namespace facepp.detection
{

	internal class PointType{
		public const string P25="25p";
		public const string P83="83p";
	}

public class Facepp_landmark 
{
		private static Facepp_landmark _instance=null;
		public static Facepp_landmark instance{
			get {
				if(_instance==null)_instance=new Facepp_landmark();
				return _instance;
			}
		}
		/// <summary>
		/// api_key
		/// </summary>
		public string api_key = "";
		/// <summary>
		/// api_secret
		/// </summary>
		public string api_secret = "";

		private const string API_URL="http://api.faceplusplus.com/detection/landmark";

		/// <summary>
		/// Gets the landmarkdata from faceID.
		/// </summary>
		/// <returns>The land mark data from face I.</returns>
		/// <param name="face_id">Face_id.</param>
		/// <param name="p_type">P_type.</param>
		public FaceLandMarkData getLandMarkDataFromFaceID(string face_id,string p_type=PointType.P25){
			return getLandMarkDataFromFaceID(this.api_key,this.api_secret,face_id,p_type);
		}
		/// <summary>
		/// Gets the landmarkdata from faceID.
		/// </summary>
		/// <returns>The land mark data from face I.</returns>
		/// <param name="face_id">Face_id.</param>
		/// <param name="p_type">P_type.</param>
		/// <param name="api_key">Api_key.</param>
		/// <param name="api_secret">Api_secret.</param>
		public FaceLandMarkData getLandMarkDataFromFaceID(string api_key,string api_secret,string face_id,string p_type=PointType.P25){
		
			string sendUrl=API_URL+"?api_key="+api_key+"&" +
				                    "api_secret="+api_secret+"&" +
				                    "face_id="+face_id+"&" +
				                    "type="+p_type;
			//获取服务器返回的信息
			string json = HttpHelper.CreateGetHttpText(sendUrl);
		
			//解码
			JsonData obj = JsonMapper.ToObject(json);
	
			FaceLandMarkData d=new FaceLandMarkData();
			d.copyObject(obj);
			return d;
		}


		/// <summary>
		/// Gets the landmarkdata from imgUrl.
		/// </summary>
		/// <returns>The land mark data from detect.</returns>
		/// <param name="imgUrl">Image URL.</param>
		/// <param name="p_type">P_type.</param>
		/// <param name="api_key">Api_key.</param>
		/// <param name="api_secret">Api_secret.</param>
		public FaceLandMarkData getLandMarkDataFromDetect(string api_key,string api_secret,string imgUrl,string p_type=PointType.P25){

			FaceDetectData detect=Facepp_detect.instance.getFaceDetectData(api_key,api_secret,imgUrl,"oneface",false);
			if(detect.faces.Count>0){
				return getLandMarkDataFromFaceID(api_key,api_secret,detect.faces[0].face_id,p_type);
			}
			return new FaceLandMarkData();
		}
		/// <summary>
		/// Gets the landmarkdata from imgUrl.
		/// </summary>
		/// <returns>The land mark data from detect.</returns>
		/// <param name="imgUrl">Image URL.</param>
		/// <param name="p_type">P_type.</param>
		public FaceLandMarkData getLandMarkDataFromDetect(string imgUrl,string p_type=PointType.P25){
			return getLandMarkDataFromDetect(this.api_key,this.api_secret,imgUrl,p_type);
		}

	
		public FaceLandMarkData getLandMarkDataFromDetect(string api_key,string api_secret,byte[] byt,string p_type=PointType.P25){

			FaceDetectData detect=Facepp_detect.instance.getFaceDetectData(api_key,api_secret,byt);
          
			if(detect.faces.Count>0){

				return getLandMarkDataFromFaceID(api_key,api_secret,detect.faces[0].face_id,p_type);
			}
			return new FaceLandMarkData();
		}
		public FaceLandMarkData getLandMarkDataFromDetect(byte[] byt,string p_type=PointType.P25){
			return getLandMarkDataFromDetect(this.api_key,this.api_secret,byt,p_type);
		}

	}



	#region Data 最终的数据
	public class FaceLandMarkData{
		private LandMark _landmark=null;
		private string _session_id="";
		private string _face_id="";

		/// <summary>
		/// G包含详细关键点分析结果，包含多个关键点的坐标。
		/// </summary>
		/// <value>The landmark.</value>
		public LandMark landmark{
		   get {return _landmark;}
		}
		/// <summary>
		/// 相应请求的session标识符，可用于结果查询
		/// </summary>
		/// <value>The session_id.</value>
		public string session_id{
			get {return _session_id;}
		}
		/// <summary>
		/// 人脸在Face++系统中的标识符
		/// </summary>
		/// <value>The face_id.</value>
		public string face_id{
			get {return _face_id;}
		}

		internal void copyObject(JsonData obj){
		
			_landmark=new LandMark();

			JsonData myobj=obj["result"][0];
		    
			_landmark.copyObject(myobj["landmark"]);
			_session_id=(string)obj["session_id"];
			_face_id=(string)myobj["face_id"];
		}
	}
	#endregion


	internal class LandMarkType{
		//一些轮廓
		public const string CONTOUR_CHIN="contour_chin";
		public const string CONTOUR_LEFT1="contour_left1";
		public const string CONTOUR_LEFT2="contour_left2";
		public const string CONTOUR_LEFT3="contour_left3";
		public const string CONTOUR_LEFT4="contour_left4";
		public const string CONTOUR_LEFT5="contour_left5";
		public const string CONTOUR_LEFT6="contour_left6";
		public const string CONTOUR_LEFT7="contour_left7";
		public const string CONTOUR_LEFT8="contour_left8";
		public const string CONTOUR_LEFT9="contour_left9";
		public const string CONTOUR_RIGHT1="contour_right1";
		public const string CONTOUR_RIGHT2="contour_right2";
		public const string CONTOUR_RIGHT3="contour_right3";
		public const string CONTOUR_RIGHT4="contour_right4";
		public const string CONTOUR_RIGHT5="contour_right5";
		public const string CONTOUR_RIGHT6="contour_right6";
		public const string CONTOUR_RIGHT7="contour_right7";
		public const string CONTOUR_RIGHT8="contour_right8";
		public const string CONTOUR_RIGHT9="contour_right9";

		//左眼
		public const string LEFT_EYE_BOTTOM="left_eye_bottom";
		public const string LEFT_EYE_CENTER="left_eye_center";
		public const string LEFT_EYE_LEFT_CORNER="left_eye_left_corner";
		public const string LEFT_EYE_LOWER_LEFT_QUARTER="left_eye_lower_left_quarter";
		public const string LEFT_EYE_LOWER_RIGHT_QUARTER="left_eye_lower_right_quarter";
		public const string LEFT_EYE_PUPIL="left_eye_pupil";
		public const string LEFT_EYE_RIGHT_CORNER="left_eye_right_corner";
		public const string LEFT_EYE_TOP="left_eye_top";
		public const string LEFT_EYE_UPPER_LEFT_QUARTER= "left_eye_upper_left_quarter";
		public const string LEFT_EYE_UPPER_RIGHT_QUARTER= "left_eye_upper_right_quarter";
		public const string LEFT_EYEBROW_LEFT_CORNER= "left_eyebrow_left_corner";
		public const string LEFT_EYEBROW_LOWER_LEFT_QUARTER="left_eyebrow_lower_left_quarter";
		public const string LEFT_EYEBROW_LOWER_MIDDLE="left_eyebrow_lower_middle";
		public const string LEFT_EYEBROW_RIGHT_CORNER="left_eyebrow_right_corner";
		public const string LEFT_EYEBROW_UPPER_LEFT_QUARTER="left_eyebrow_upper_left_quarter";
		public const string LEFT_EYEBROW_UPPER_MIDDLE="left_eyebrow_upper_middle";
		public const string LEFT_EYEBROW_UPPER_RIGHT_QUARTER="left_eyebrow_upper_right_quarter";
		//右眼
		public const string RIGHT_EYE_BOTTOM="right_eye_bottom";
		public const string RIGHT_EYE_CENTER="right_eye_center";
		public const string RIGHT_EYE_LEFT_CORNER="right_eye_left_corner";
		public const string RIGHT_EYE_LOWER_LEFT_QUARTER="right_eye_lower_left_quarter";
		public const string RIGHT_EYE_LOWER_RIGHT_QUARTER="right_eye_lower_right_quarter";
		public const string RIGHT_EYE_PUPIL="right_eye_pupil";
		public const string RIGHT_EYE_RIGHT_CORNER="right_eye_right_corner";
		public const string RIGHT_EYE_TOP="right_eye_top";
		public const string RIGHT_EYE_UPPER_LEFT_QUARTER= "right_eye_upper_left_quarter";
		public const string RIGHT_EYE_UPPER_RIGHT_QUARTER= "right_eye_upper_right_quarter";
		public const string RIGHT_EYEBROW_LEFT_CORNER= "right_eyebrow_left_corner";
		public const string RIGHT_EYEBROW_LOWER_LEFT_QUARTER="right_eyebrow_lower_left_quarter";
		public const string RIGHT_EYEBROW_LOWER_MIDDLE="right_eyebrow_lower_middle";
		public const string RIGHT_EYEBROW_RIGHT_CORNER="right_eyebrow_right_corner";
		public const string RIGHT_EYEBROW_UPPER_LEFT_QUARTER="right_eyebrow_upper_left_quarter";
		public const string RIGHT_EYEBROW_UPPER_MIDDLE="right_eyebrow_upper_middle";
		public const string RIGHT_EYEBROW_UPPER_RIGHT_QUARTER="right_eyebrow_upper_right_quarter";



		//口
		public const string MOUTH_LEFT_CORNER="mouth_left_corner";
		public const string MOUTH_LOWER_LIP_BOTTOM="mouth_lower_lip_bottom";
		public const string MOUTH_LOWER_LIP_LEFT_CONTOUR1="mouth_lower_lip_left_contour1";
		public const string MOUTH_LOWER_LIP_LEFT_CONTOUR2="mouth_lower_lip_left_contour2";
		public const string MOUTH_LOWER_LIP_LEFT_CONTOUR3="mouth_lower_lip_left_contour3";
		public const string MOUTH_LOWER_LIP_RIGHT_CONTOUR1="mouth_lower_lip_right_contour1";
		public const string MOUTH_LOWER_LIP_RIGHT_CONTOUR2="mouth_lower_lip_right_contour2";
		public const string MOUTH_LOWER_LIP_RIGHT_CONTOUR3="mouth_lower_lip_right_contour3";
		public const string MOUTH_UPPER_LIP_LEFT_CONTOUR1="mouth_upper_lip_left_contour1";
		public const string MOUTH_UPPER_LIP_LEFT_CONTOUR2="mouth_upper_lip_left_contour2";
		public const string MOUTH_UPPER_LIP_LEFT_CONTOUR3="mouth_upper_lip_left_contour3";
		public const string MOUTH_UPPER_LIP_RIGHT_CONTOUR1="mouth_upper_lip_left_contour1";
		public const string MOUTH_UPPER_LIP_RIGHT_CONTOUR2="mouth_upper_lip_left_contour2";
		public const string MOUTH_UPPER_LIP_RIGHT_CONTOUR3="mouth_upper_lip_left_contour3";
		public const string MOUTH_LOWER_LIP_TOP="mouth_lower_lip_top";
		public const string MOUTH_RIGHT_CORNER="mouth_right_corner";
		public const string MOUTH_UPPER_LIP_BOTTOM="mouth_upper_lip_bottom";
		public const string MOUTH_UPPER_LIP_TOP="mouth_upper_lip_top";
		//鼻子
		public const string NOSE_LEFT="nose_left";
		public const string NOSE_RIGHT="nose_right";
		public const string NOSE_TIP="nose_tip";
		public const string NOSE_CONTOUR_LOWER_MIDDLE="nose_contour_lower_middle";
		public const string NOSE_CONTOUR_LEFT1="nose_contour_left1";
		public const string NOSE_CONTOUR_LEFT2="nose_contour_left2";
		public const string NOSE_CONTOUR_LEFT3="nose_contour_left3";
		public const string NOSE_CONTOUR_RIGHT1="nose_contour_left1";
		public const string NOSE_CONTOUR_RIGHT2="nose_contour_right2";
		public const string NOSE_CONTOUR_RIGHT3="nose_contour_right3";

	}

	#region LandMark 包含详细关键点分析结果，包含多个关键点的坐标。
	public class LandMark
	{
		private List<KeyPoint> _keyPoints=null;
		private IDictionary<string,JsonData> _dictionarys=null;

		/// <summary>
		/// 关键点集合
		/// </summary>
		/// <value>The key points.</value>
		public List<KeyPoint> keyPoints{
			get {return _keyPoints;}
		}
		/// <summary>
		/// 根据type返回包含 x y 的P 
		/// </summary>
		/// <returns>The p.</returns>
		/// <param name="type">Type.(LandMarkType)</param>
		public KeyPoint getP(string type) {
			if(_dictionarys==null)return new KeyPoint();
			JsonData vd=_dictionarys[type];
			KeyPoint kp=new KeyPoint();
			kp._name=type;
			kp._relative=new Vector2();
			kp._relative.x=(float)(double)vd["x"];
			kp._relative.y=(float)(double)vd["y"];
			return kp;
		}



		internal void copyObject(JsonData obj){
			_keyPoints=new List<KeyPoint>();
			_dictionarys=obj.Inst_Object;
			foreach(KeyValuePair<string,JsonData> kv in _dictionarys){
				KeyPoint k=new KeyPoint();
				k._name=kv.Key;
				k._relative=new Vector2();
				JsonData jdata=kv.Value;
				k._relative.x=(float)(double) jdata["x"];
				k._relative.y=(float)(double) jdata["y"];
				_keyPoints.Add(k);
			}

		}
	}
	#endregion 

	public class KeyPoint{
		internal string _name="";
		public string name{get {return _name;}}

		internal Vector2 _relative=Vector2.zero;
		public Vector2 relative{get {return _relative;}}


		public Vector3 postionRelative{
			get{
				float x=(_relative.x-50)/100;
				float y=(100-_relative.y)/100;
				return new Vector2(x,y);
			}
		}
		public void aa(){
			//_relative.
		}

	}


}
